package org.boulderfoodrescue.webapp.client.pickups.enterpickupreport;

import org.boulderfoodrescue.webapp.client.pickups.selectors.SelectorInterface;

import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.shared.DateTimeFormat;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.cellview.client.SimplePager.TextLocation;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

public class SpecificPickupTable extends FlowPanel implements SelectorInterface<SpecificPickup>{
	protected CellTable<SpecificPickup> table;
	protected ListDataProvider<SpecificPickup> dataProvider;
	private SpecificPickup value;
	private SimplePager pager;
	
	public SpecificPickupTable(){
		dataProvider = new ListDataProvider<SpecificPickup>();
		table = new CellTable<SpecificPickup>();

		/* Create all the columns */

		TextColumn<SpecificPickup> indexColum = new TextColumn<SpecificPickup>(){
			@Override
			public String getValue(SpecificPickup object) {
				return object.getPickupInSeries() + "";
			}
		};
		
		TextColumn<SpecificPickup> dateColumn = new TextColumn<SpecificPickup>(){
			@Override
			public String getValue(SpecificPickup object) {
				return object.getDateAsString(DateTimeFormat.PredefinedFormat.DATE_SHORT);
			}
		};

		/* Add the columns to the table */
		table.addColumn(indexColum, "index");
		table.addColumn(dateColumn, "Date");

		dataProvider.addDataDisplay(table);

		ListHandler<SpecificPickup> sortHandler = new ListHandler<SpecificPickup>(dataProvider.getList());
		table.addColumnSortHandler(sortHandler);

//		/* Set the columns as sortable and add their comparators to the sort handler */
//		dateColumn.setSortable(true);
//		sortHandler.setComparator(dateColumn, new Comparator<SpecificPickup>(){
//			@Override
//			public int compare(SpecificPickup o1, SpecificPickup o2) {
//				return o1.getName().compareTo(o2.getName());
//			}
//		});

		final SingleSelectionModel<SpecificPickup> selectionModel = new SingleSelectionModel<SpecificPickup>();
		table.setSelectionModel(selectionModel);
		selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
			@Override
			public void onSelectionChange(SelectionChangeEvent event) {
				value = selectionModel.getSelectedObject();
			}
		});

		SimplePager.Resources pagerResources = GWT.create(SimplePager.Resources.class);
		pager = new SimplePager(TextLocation.CENTER, pagerResources, false, 0, true);
		pager.setDisplay(table);
		pager.setPageSize(10);


		this.add(table);
		this.add(pager);
		table.setWidth("100%");
		//this.setHeight("100%");
		table.setEmptyTableWidget(new HTML("No locations are available to view."));
	}
	@Override
	public SpecificPickup getValue() {
		return value;
	}
	@Override
	public FlowPanel getTable() {
		return this;
	}
	@Override
	public void setValue(SpecificPickup value) {
		this.value = value;
	}
	@Override
	public String getValueAsString() {
		if(value == null)
			return "Null value";
		return value.getDateAsString(DateTimeFormat.PredefinedFormat.DATE_MEDIUM);
	}
	@Override
	public void addEntry(SpecificPickup entry) {
		dataProvider.getList().add(entry);
	}
	@Override
	public void removeEntry(SpecificPickup entry) {
		dataProvider.getList().remove(entry);
	}
	@Override
	public void refreshEntries() {
		table.redraw();
	}
	
	@Override
	public void clear(){
		dataProvider.getList().clear();
	}
}
